#include "moveIntf.h"
using namespace infMove;
/*mover--------------------------------
 * a class that describes all things
 * that can move.  lists the mehtods
 * needed to move things around and keep track
 * of their position
 * ----------------------------------*/
//constructor-----------
mover::mover(){}
mover::~mover(){
    if(parent != NULL) parent->remove(this);
}
//access----------------
direction mover::gFacing(){
    return facing;
}
void mover::gPos(int* x, int* y){
    *x = posx;
    *y = posy;
}
int mover::posX(){
    return posx;
}
int mover::posY(){
    return posy;
}
//manips----------------
void mover::move(int x, int y){
    if(parent != NULL){
        parent->move(posx, posy, x, y);
    }
    posx = x;
    posy = y;
}
void mover::place(int x, int y, movemap* m){
    posx = x;
    posy = y;
    if(m != NULL){
        parent = m;
        parent->add(this);
    }
}
void mover::face(direction f){
    facing = f;
}
void mover::setMap(movemap* m){
    if(parent != NULL) parent->remove(this);
    parent = m;
    if(parent != NULL) parent->add(this);
}
/*movemap------------------------------
 * Holds all the moveables in a map;
 * allows for movers to move around the map
 *-----------------------------------*/
//constructors----------
movemap::movemap(){}
movemap::~movemap(){}
//access----------------
int movemap::size(){
    return mapping.size();
}
mover* movemap::get(int x, int y){
    pair<int, int> pt = make_pair(x,y);
    if(mapping.find(pt) != map.end()) return mapping[make_pair(x,y)];
    return NULL;
}
//manips----------------
mover* movemap::move(int x, int y, int nx, int ny){
    pair<int, int> pt = make_pair(nx, ny);
    mover* m = get(x,y);
    remove(m);
    mapping[pt] = m;
}
mover* movemap::add(mover* m){
    int x, y;
    m->gPos(&x, &y);
    pair<int, int> p = make_pair(x,y);
    mapping[p] = m;
    return mapping[p];
}
mover* movemap::remove(mover* m){
    int x, y;
    m->gPos(&x, &y);
    pair<int, int> p = make_pair(x,y);
    mover* m2 = mapping[p];
    mapping.erase(p);
    return m2;
}
